<?php
// +----------------------------------------------------------------------
// | OneThink [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.onethink.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: 麦当苗儿 <zuojiazi@vip.qq.com> <http://www.zjzit.cn>
// +----------------------------------------------------------------------

namespace Card\Controller;

/**
 * 前台首页控制器
 * 主要获取首页聚合数据
 */
class IndexController extends CardController {

	//系统首页
    public function index(){
        $this->status403();
    }

    //系统首页
    public function card() {
        $id = I('get.id', 0, 'intval');
        $userModel = M('User');
        $where = array();
        if ($id > 0) {
            $where['id'] = $id;
        }
        $userInfo = $userModel->where($where)->find();
        if (!$userInfo) {
          return $this->error('用户不存在', '/add.html');
        }
        $this->assign('info', $userInfo);
        $this->display('card');
    }

    public function qrurl() {
        require_once API_PATH.'Other/qrcode/phpqrcode.php';
        $dao = M('User');
        $id = I('get.id', 0, 'intval');
        $info = $dao->find($id);
        if (empty($info)) {
            return $this->status403();
        }
        $value = C('CARD_DOMAIN').'/card/' . $id; //二维码内容
        $errorCorrectionLevel = 'L';
        //容错级别
        $matrixPointSize = 11;
        //生成图片大小
        $logo = empty($info['head_img']) ? false : realpath(__ROOT__).get_cover($info['head_img'], 'path');
        $QR = $logo ? C('CARD_QCODE_PATH').'temp_qrcode.png' : false;
        $QR = false;
        //生成二维码图片
        \QRcode::png($value, $QR, $errorCorrectionLevel, $matrixPointSize, 2);

        //已经生成的原始二维码图
        if ($logo !== false) {
            $QR = imagecreatefromstring(file_get_contents($QR));
            $logo = imagecreatefromstring(file_get_contents($logo));
            $QR_width = imagesx($QR);
            //二维码图片宽度
            $QR_height = imagesy($QR);
            //二维码图片高度
            $logo_width = imagesx($logo);
            //logo图片宽度
            $logo_height = imagesy($logo);
            //logo图片高度
            $logo_qr_width = $QR_width / 5;
            $scale = $logo_width / $logo_qr_width;
            $logo_qr_height = $logo_height / $scale;
            $from_width = ($QR_width - $logo_qr_width) / 2;
            //重新组合图片并调整大小
            imagecopyresampled($QR, $logo, $from_width, $from_width, 0, 0, $logo_qr_width, $logo_qr_height, $logo_width, $logo_height);
            //输出图片
            Header("Content-type: image/png");
            ImagePng($QR);
        }
    }


    public function qrvcf()
    {
      $id = I('id', 0, 'intval');
      $info = M('User')->find($id);
      if (empty($info)) {
          return $this->status403();
      }
      if (preg_match("/\.[jpg|png|jpeg]/", __SELF__)) {
        return $this->_qrvcf();
      }
      $imgurl = __SELF__.'.png';
      $this->assign('imgurl', $imgurl);
      $this->assign('info', $info);
      $this->display('qrvcf');
    }


    /**
     * 微信扫码的名片信息 H5的url
     */
    protected function _qrvcf() {
        require_once API_PATH.'Other/qrcode/phpqrcode.php';
        $dao = M('User');

        $id = intval($_GET['id']);
        $info = $dao->find($id);
        if (empty($info)) {
            return $this->status403();
        }
        $vcard = array(
            'vname' => $info['name'],
            'vtel' => $info['telephone'],
            'vemail' => $info['email'],
            'vaddress' => $info['address'],
        );

        if ($info['head_img']) {
            $info['head_img'] = get_cover($info['head_img'], 'path');
        }
        $chl = "BEGIN:VCARD\nVERSION:3.0" . //vcard头信息
                "\nFN:" . $vcard['vname'] .
                "\nTEL:" . $vcard['vtel'] .
                "\nEMAIL:" . $vcard['vemail'] .
                "\nADR;WORK:" . $vcard['vaddress'];
        if ($info['company']) {
            $chl.="\nORG:" . $info['company'];
        }
        if ($info['position']) {
            $chl.="\nTITLE:" . $info['position'];
        }
        if ($info['department']) {
            $chl.="\nROLE:" . $info['department'];
        }
        if ($info['homepage_url']) {
            $chl.="\nURL:" . $info['homepage_url'];
        }
        if ($info['nickname']) {
            $chl.="\nNICKNAME:" . $info['nickname'];
        }
        if ($info['moblie_phone']) {
            $chl.="\nTEL;CELL;VOICE:" . $info['moblie_phone'];
        }
        if ($info['head_img']) {
            $chl.="\nPHOTO;VALUE=URL;TYPE=GIF:".C('CARD_DOMAIN') . $info['head_img'];
        }

        $chl.="\nEND:VCARD";
        //vcard尾信息
        $errorCorrectionLevel = 'L';
        //容错级别
        $matrixPointSize = 6;
        //生成图片大小
        //生成二维码图片
        $logo = empty($info['head_img']) ? false : realpath(__ROOT__).$info['head_img'];
        $QR = $logo ? C('CARD_QCODE_PATH').'temp_qrcode.png' : false;
        $QR = false;
        //生成二维码图片
        \QRcode::png($chl, $QR, $errorCorrectionLevel, $matrixPointSize, 2);
        //已经生成的原始二维码图
        if ($logo !== false) {
            $QR = imagecreatefromstring(file_get_contents($QR));
            $logo = imagecreatefromstring(file_get_contents($logo));
            $QR_width = imagesx($QR);
            //二维码图片宽度
            $QR_height = imagesy($QR);
            //二维码图片高度
            $logo_width = imagesx($logo);
            //logo图片宽度
            $logo_height = imagesy($logo);
            //logo图片高度
            $logo_qr_width = $QR_width / 5;
            $scale = $logo_width / $logo_qr_width;
            $logo_qr_height = $logo_height / $scale;
            $from_width = ($QR_width - $logo_qr_width) / 2;
            //重新组合图片并调整大小
            imagecopyresampled($QR, $logo, $from_width, $from_width, 0, 0, $logo_qr_width, $logo_qr_height, $logo_width, $logo_height);
            //输出图片
            Header("Content-type: image/png");
            ImagePng($QR);
        }
    }

    /**
     * 发送vcf文件
     */
    public function vcf($id = null) {
        if ($id == null) {
            return $this->status403();
        }
        require_once API_PATH.'SDK/VCardIFL/VCardIFL.class.php';
        $data = M('User')->find($id);
        $arData = array();
        $arData['fileName'] = $data['name'] . '_' . $data['mobile'];
        $arData['vcard_f_name'] = $data['name'];
        $arData['vcard_c_mobile'] = $data['mobile'];
        $arData['vcard_compan'] = $data['compan'];
        $arData['vcard_birtda'] = $data['birtday'];
        $arData['vcard_h_mail'] = $data['email'];
        $arData['vcard_h_zip'] = $data['zip'];
        $arData['vcard_h_phon'] = $data['tel'];
        $arData['vcard_h_addr'] = $data['addr'];
        $card = new \VCardIFL($arData);
        $card->createVcard();
        $card->DownloadVcard();
    }

}
